package com.example.web.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.example.common.Interface.user.entity.dto.*;
import com.example.common.Interface.user.entity.vo.UserInfoVO;
import com.example.common.Interface.user.service.RolePermissionService;
import com.example.common.Interface.user.service.UserService;
import com.example.common.response.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @DubboReference(check = false)
    private UserService userService;

    @DubboReference(check = false)
    private RolePermissionService rolePermissionService;

    @PostMapping("/deliver")
    public ResponseResult<DeliveryResp> deliverToken(@RequestBody DeliverTokenReq req) {
        log.info("deliverToken request: {}", req);
        if(req == null || req.getUserId() == null){
            return ResponseResult.fail("请求为空");
        }
        StpUtil.login(req.getUserId());
        String token = StpUtil.getTokenValue();
        return ResponseResult.success(DeliveryResp.builder().token(token).build());
    }
    @PostMapping("/verify")
    public ResponseResult<VerifyResp> verifyToken(@RequestBody VerifyTokenReq req) {
        if(req == null || req.getToken() == null){
            return ResponseResult.fail("请求为空");
        }
        StpUtil.renewTimeout(req.getToken(), 30 * 60);
        return ResponseResult.success("有效期已刷新");
    }

    @PostMapping("/register")
    public ResponseResult<RegisterRespDTO> register(@RequestBody RegisterReqDTO request) {
        log.info("register request: {}", request);
        if(request == null){
            return ResponseResult.fail("请求为空");
        }
        var resp = userService.register(request);
        rolePermissionService.assignRolesToUser(resp.getUserId(), List.of(1L));
        return ResponseResult.success(resp);
    }

    @PostMapping("/login")
    public ResponseResult<LoginRespDTO> login(@RequestBody LoginReqDTO request) {
        if(request == null){
            return ResponseResult.fail("请求为空");
        }
        var resp = userService.login(request);
        return ResponseResult.success(resp);
    }

    @PostMapping("/logout/{userid}")
    public ResponseResult<Boolean> logout(@PathVariable Long userId) {
        if(userId == null){
            return ResponseResult.fail("请求为空");
        }
        var resp = userService.logout(userId);
        if(!resp){
            return ResponseResult.fail("登出失败");
        }
        StpUtil.logout(userId);
        return ResponseResult.success(true);
    }

    @DeleteMapping("/delete/{userId}")
    public ResponseResult<Boolean> deleteUser(@PathVariable("userId") Long userId) {
        if(userId == null){
            return ResponseResult.fail("请求为空");
        }
        var resp =  userService.deleteUser(userId);
        return ResponseResult.success(resp);
    }

    @PutMapping("/update/{userId}")
    public ResponseResult<Boolean> updateUser(@PathVariable Long userId, @RequestBody UpdateReqDTO user) {
        var resp= userService.updateUser(userId, user);
        return ResponseResult.success(resp);
    }

    @GetMapping("/get/{userId}")
    public ResponseResult<UserInfoVO> getUserInfo(@PathVariable Long userId) {
        if(userId == null){
            return ResponseResult.fail("请求为空");
        }
        var resp = userService.getUserInfo(userId);
        return ResponseResult.success(resp);
    }
}
